<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <META HTTP-EQUIV="pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-store, must-revalidate">
    <META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
    <META HTTP-EQUIV="expires" CONTENT="0">
    <title>返工单管理</title>
    <link rel="stylesheet" type="text/css" href="./css/main.css" />
    <link rel="stylesheet" type="text/css" href="./layui/css/layui.css" />
    <link rel="stylesheet" type="text/css" href="./css/layuiAdminTheme.css" />
    <style type="text/css">
        .layui-form-label {
            width: auto !important;
        }

        .operation span:last-of-type {
            color: #f5222d;
        }

        .pageNav {
            margin: 0 auto;
            width: fit-content;
        }

        .pageNav a,
        .operation span {
            cursor: pointer;
            display: inline-block;
        }

        cite {
            cursor: default;
        }
        td {
            white-space: nowrap;
            text-align: center;
        }
    </style>
</head>
<body>
<script src="./js/main.js" charset="utf-8"></script>
<script src="./layui/layui.js" charset="utf-8"></script>
<script src="./js/jQuery.main.js"></script>

<form class="layui-form" action="" lay-filter="data">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">搜索：</label>
            <input type="text" name="reLot" id="reLot" class="layui-input" style="width: 15em;" placeholder="返工单编号" value="">
        </div>
        <div class="layui-inline">
            <input type="text" name="orderNum" id="orderNum" class="layui-input" style="width: 15em;" placeholder="原工单编号" value="">
        </div>
        <div class="layui-inline">
            <!--          <input id="deptName" type="text" name="orderDept" class="layui-input" style="width: 8em;" placeholder="所属部门" value="" >-->
            <select id="deptName" type="text" name="orderDept" class="layui-input" style="width: 8em;" lay-filter="orderDept" onclick="isAdminOfOrder()">
                <option value="">请选择部门...</option>
            </select>
        </div>
        <div class="layui-inline">
            <select id="checkStatus" type="text" name="checkStatus" class="layui-input" style="width: 8em;" lay-filter="checkStatus">
                <option value="">请选择通过状态...</option>
                <option value="0">未审核</option>
                <option value="1">审核通过</option>
                <option value="2">单据作废</option>
            </select>
        </div>
        <div class="layui-inline">
            <input type="text" name="displayLength" class="layui-input" style="display: none;" placeholder="每页页数" value="10">
            <button type="submit" id="searchBtn" class="layui-btn" lay-submit="" lay-filter="searchBtn">搜索返工单记录</button>
        </div>
    </div>
</form>

<table class="layui-table" lay-skin="line" lay-filter="checkInfo">
    <thead>
    <tr>
        <th style="text-align: center">返工单号</th>
        <th style="text-align: center">原工单号</th>
        <th style="text-align: center">提交时间</th>
        <th style="text-align: center">提交人</th>
        <th style="text-align: center">是否列印</th>
        <th style="text-align: center">审核状态</th>
        <th style="text-align: center">操作</th>
    </tr>
    </thead>
    <tbody></tbody>
</table>
<div style="display: flex;">
    <div style="flex: 1">
        <div class="pageNav">
          <span class="layui-breadcrumb" lay-separator="-">
            <a class="fristPage">首页</a>
            <a class="lastPage">上一页</a>
            <a><cite>第<span id="page"></span>页</cite></a>
            <a class="nextPage">下一页</a>
            <a class="endPage">尾页</a>
          </span>
        </div>
    </div>
</div>

<script type="text/javascript">
    var displayStart = 1;
    var endPage = null;
    var layer = layui.layer;
    var form = null;
    var list = null;

    function selectAll() {
        $("input[name=downSelect]:not(:disabled)").each(function(){
            this.checked = $("#select")[0].checked
        });
        // $('input[type=checkbox]').each(function() {
        //     var disabled= $("#select")[0].attr("disabled");//获取当前对象的disabled属性
        //     if(disabled==false){ //判断如果是非disabled则进行处理
        //         this.checked = $("#select")[0].checked
        //     }
        // })
    }

    layui.use(['form', 'element'], function() {
        form = layui.form;
        search(1,'desc');
        form.on('submit(searchBtn)', function() {
            search(1,'desc');
            return false;
        });
        $('.fristPage').click(function() {
            search(1,'desc');
            return false;
        });
        $('.lastPage').click(function() {
            search((displayStart > 1 ? displayStart - 1 : 1),'desc');
            return false;
        });
        $('.nextPage').click(function() {
            search((displayStart < endPage ? displayStart + 1 : endPage),'desc');
            return false;
        });
        $('.endPage').click(function() {
            search(endPage,'desc');
            return false;
        })
        $.ajax({
            url: url + '/sys-dept/listDept',
            method: 'POST',
            async : false,
            contentType: 'application/json',
            success: function(res) {
                //显示前先判断是否为管理员
                $.ajax({
                    url: '/sys-user/getAdmin/'+sessionStorage.getItem('userId'),
                    method: 'GET',
                    async : false,
                    contentType: 'application/json',
                    success: function (res) {
                        if (res.code===200){
                            isAd=1;
                        }
                        else if (res.code===400){
                            isAd=0;
                            thisDept=res.data.dept;
                        }
                    }
                })
                //按照是否为管理员显示下拉框
                if (res.code == 200) {
                    for (var i = 0; i < res.data.length; i++) {
                        if(isAd===1){
                            $("#deptName").append("<option value='" + res.data[i].id + "'>" + res.data[i].dept +"</option>");
                        }else if (isAd===0&&res.data[i].dept===thisDept){
                            $("#deptName").append("<option selected='selected' value='" + res.data[i].id + "'>" + res.data[i].dept +"</option>").attr('disabled','disabled');
                        }
                    }
                    form.render('select');
                }
            }
        })
    })

    //时间排序方法
    function sortTimeOfCheck(data) {
        search(1,data)
    }

    function checkReOrder(data) {
        $.ajax({
            url: url + '/re-order/checkStatus',
            method: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({
                loginName:sessionStorage.getItem('loginName'),
                reLot:data
            }),
            success: function(res) {
                if(res.code==200){
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-rim',
                        area: ['360px', '260px'],
                        title: '提交审核意见',
                        content: `
                <form class="layui-form" action="">
                <div class="layui-row" style="margin-bottom: 10px"></div>
                <input id="orderId" type="text" name="id" style="display: none;">

                <div class="layui-input-inline" style="margin: 5px;">
                <label class="layui-form-label">返工单号：</label>
                <div class="layui-input-inline " style="width: 15em"  >
                <input id="reLotOfCheck" type="text" name="reLotOfCheck" lay-verify="required" class="layui-input" style="margin-bottom: 10px ; width: 12em !important;" readonly="readonly">
                </div></div>

                <div class="layui-input-inline layui-form-item" style="margin: 5px;">
                <label class="layui-form-label">审核意见：</label>
                <div class="layui-input-inline">
                    <input type="radio" name="checkStatus" value="1" title="审核通过" checked>
                    <input type="radio" name="checkStatus" value="2" title="作废处理">
                </div></div>

                <div align="center" style="margin: 15px">
                <button id="subCheck" type="button" class="layui-btn" lay-submit="" lay-filter="subCheck" >提交意见</button>
                <button type="button" class="layui-btn layui-btn-danger" onclick="layer.close(layer.index)">取消</button></div>
                </form>
          `,
                        success: function(){
                            $('#reLotOfCheck').val(data);
                            form.render();
                        }
                    });
                    form.on('submit(subCheck)', function() {
                        $.ajax({
                            url: url + '/re-order/checkIt',
                            method: 'POST',
                            contentType: 'application/json',
                            data: JSON.stringify({
                                reLot:data,
                                status:$('input[name="checkStatus"]:checked').val()
                            }),
                            success: function(res) {
                                layer.closeAll();
                                if(res.code==200){
                                    layer.alert(res.msg, {
                                        icon: 1,
                                        skin: 'layer-ext-demo' //见：扩展说明
                                    })
                                }else{
                                    layer.alert(res.msg, {
                                        icon: 2,
                                        skin: 'layer-ext-demo' //见：扩展说明
                                    })
                                }
                                $('#searchBtn').click();
                            }
                        })
                    })
                }else {
                    layer.alert(res.msg, {
                        icon: 2,
                        skin: 'layer-ext-demo' //见：扩展说明
                    })
                }
            }
        })
    }

    function downReOrder(data) {
        $.ajax({
            url: url + '/re-order/downStatus',
            method: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({
                reLot:data
            }),
            success: function(res) {
                if(res.code==200){
                    var str=data+"&"+sessionStorage.getItem('loginName')
                    let confirm1=layer.confirm('点击确定开始导出列印！', {
                        btn: ['确认','取消']
                    },function () {
                        for(var i=0;i<list.length;i++){
                            //window.open(url+'/excel/exportTemplateData/'+list[i]);
                            layer.open({
                                type: 2,
                                title: '下载提示框',
                                shadeClose: true,
                                shade: false,
                                maxmin: true, //开启最大化最小化按钮
                                area: ['300px', '300px'],
                                content: url+'/excel/exportReOrder/'+str
                            });
                            layer.close(confirm1);
                        }
                        layer.alert('下载中...请稍等...', {
                            icon: 1,
                            skin: 'layer-ext-demo', //见：扩展说明
                            end: function () {
                                //关闭弹出
                                layer.closeAll('iframe');
                                $('#searchBtn').click()
                            }
                        })
                    },function (){
                        layer.close(confirm1);
                    })
                }else {
                    layer.alert(res.msg, {
                        icon: 2,
                        skin: 'layer-ext-demo' //见：扩展说明
                    })
                }
            }
        })
    }

    function downReStatus(data) {
        switch (data) {
            case true:
                return "<span style='color:#5FB878;'>已列印</span>"
            case false:
                return "<span style='color:#FF5722;'>未列印</span>"
        }
    }
    function checkReStatus(data) {
        switch (data) {
            case "1":
                return "<span style='color:#5FB878;'>审核通过</span>"
            case "0":
                return "<span style='color:#FFB800;'>未审核</span>"
            case "2":
                return "<span style='color:#FF5722;'>单据作废</span>"
        }
    }


    function search(index) {
        if (!index) var index = displayStart;
        var data = form.val('data');
        data.displayStart = index;
        data.userId=sessionStorage.getItem('userId');
        data.oldLot=$('#orderNum').val();
        data.reLot=$('#reLot').val();
        data.status=$('#checkStatus').val();
        data.orderDept=$('#deptName').val();
        //data.desc=status;
        $.ajax({
            url: url + '/re-order/listReOrder',
            method: 'POST',
            contentType: 'application/json',
            data: JSON.stringify(data),
            success: function(res) {
                $('tbody').empty();
                displayStart = res.data.current;
                endPage = res.data.pages;
                list = res.data.records;
                for (var i = 0; i < res.data.records.length; i++) {
                    $('tbody').append(
                        "<tr>" +
                        "<td style='color:#1E9FFF;'>" + res.data.records[i].reLot + "</td>" +
                        "<td style='color: #1E9FFF;'>" + res.data.records[i].oldLot +"</td>" +
                        "<td style='color:#1E9FFF;'>" + res.data.records[i].createTime.replace('T', ' ') + "</td>" +
                        "<td style='color:#1E9FFF;'>" + res.data.records[i].creater + "</td>" +
                        "<td>" +downReStatus(res.data.records[i].isDown) + "</td>" +
                        "<td>" +checkReStatus(res.data.records[i].status) + "</td>" +
                        "<td class='operation'>" +
                        "<button type='button' class='layui-btn layui-btn-normal layui-btn-sm' onclick='checkReOrder(\"" + res.data.records[i].reLot + "\")'>审核单据</button>" +
                        "<button type='button' style='background-color: #009688' class='layui-btn layui-btn-sm' onclick='downReOrder(\"" + res.data.records[i].reLot + "\")'>列印单据</button>" +
                        "</td></tr>"
                    );
                }
                $('#page').html(displayStart)
            }
        })
    }
</script>
</body>
</html>